/********************************************************************************
 * Copyright(c) 2020-2025 VINCENT_WY All rights reserved.
 * - Filename
 * - Author  Vincent
 * - Version V1.0.0
 * - Date    2023/09/21
 * - Brief
 * - FunctionList:
 ******************************************************************************
 * History:
 *
 *
 *
 ******************************************************************************
 */

#pragma once

#include "IReplayImpl.hpp"
#include "CameraFrame.hpp"
#include "FileReader.h"
#include <condition_variable>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
// #include "SafeQueue.hpp"

class ReplayImpl : public IReplayImpl{
public:
  ReplayImpl(const int &id, const std::string &path);
  ~ReplayImpl() override;
  void start(const std::function<void(std::shared_ptr<CameraFrame> &&)> &,
             const std::function<void(std::shared_ptr<BevFrame> &&)> &,const long long int &time) override;
  void stop() override;
  bool isComplete() override;
  void doRepeat() override;
  long long int getFirstFrameTm() override;

private:
  enum class Status { START = 0, REPEAT, CONTINUE, IDLE };
  std::string Tag{"[ReplayImpl]:"};
  bool _isExit{false};
  bool _isComplete{false};
  int _cameraId;
  long long int _startTime{0};
  std::string _root{};
  std::shared_ptr<FileReader> _fileReaderPtr;
  std::mutex _frameMtx;
  std::condition_variable _frameCv;
  std::deque<std::shared_ptr<CameraFrame>> _frameDqu;
  std::vector<FileInfo> _fileInfoList;
  Status _status{Status::IDLE};
  std::unique_ptr<std::thread> _readLoop{nullptr};
  std::unique_ptr<std::thread> _invokeLoop{nullptr};
  void ReadLoop();
  void InvokeLoop(const std::function<void(std::shared_ptr<CameraFrame> &&)> &, const std::function<void(std::shared_ptr<BevFrame> &&)> &);
};